package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    @Autowired
    private OverflowListDao overflowListDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr) {
        overflowListDao.save(overflowList);
        Gson gson = new Gson();
        List<OverflowListGoods> overflowListGoodsList = gson.fromJson(overflowListGoodsStr, new TypeToken<List<OverflowListGoods>>() {}.getType());

        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        overflowList.setUserId(user.getUserId());

        overflowListGoodsList.stream().peek(overflowListGoods -> {
            overflowListGoods.setOverflowListId(overflowList.getOverflowListId());
            overflowListGoodsDao.insertOverflowListGoods(overflowListGoods);
            Goods goods = goodsDao.getGoodsByGoodsId(overflowListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity() + overflowListGoods.getGoodsNum());
            goodsDao.update(goods);

        }).collect(Collectors.toList());
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        List<OverflowList> overflowLists = overflowListDao.list(sTime,eTime);
        Map<String,Object> map = new HashMap<>();
        overflowLists.stream().peek(overflowList -> {
            User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
            overflowList.setUserId(currentUser.getUserId());
            overflowList.setTrueName(currentUser.getTrueName());
        }).collect(Collectors.toList());
        map.put("rows",overflowLists);
        return null;
    }

    @Override
    public Map<String, Object> goodsList(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoodsList = overflowListGoodsDao.goodsList(overflowListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",overflowListGoodsList);
        return map;
    }
}
